import Vue from 'vue'
import App from './App'
import md5 from '@/static/utils/md5.min.js'
// 参数排序
function objKeySort(obj) {
	var newkey = Object.keys(obj).sort();
	var newObj = {};
	for (var i = 0; i < newkey.length; i++) {
			newObj[newkey[i]] = obj[newkey[i]];
	}
	return newObj;
}

function sign() {
	let data = {}
	// #ifdef H5
	let ua = navigator.userAgent.toLowerCase()
	if (ua.match(/MicroMessenger/i) == "micromessenger") {
		data.port = 'wechat'
	}else{
		data.port = 'h5'
	}
	// #endif
	// #ifdef MP-WEIXIN
	data.port = 'wechat'
	// #endif
	// #ifdef APP-PLUS
	switch(uni.getSystemInfoSync().platform){
		case 'android':
			data.port = 'android'
		  break;
		case 'ios':
			data.port = 'ios'
		  break;
	}
	// #endif
	data.timestamp = Math.round(new Date() / 1000)
	// 获取签名开始
	let obj = 'jjds2020+'
	let newObj = objKeySort(data)
	for (let i in newObj){
		obj += i + '=' + newObj[i] +'&'
	}
	obj = obj.substring(0, obj.length - 1)
	obj += '+jjds2020'
	obj = md5.hexMD5(obj)
	obj = obj.toUpperCase()
	// 获取签名结束
	data.sign = obj
	data.key = uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).token:''
	// #ifdef MP-WEIXIN
	data.appid = uni.getExtConfigSync().appid
	// #endif
	return data
}

import store from "./store";

function showLoading(show) {
  store.commit("showLoading", show);
}

Vue.prototype.$store = store;

Vue.prototype.$showLoading = showLoading;

Vue.config.productionTip = false

let baseUrl
let isTest = false
if(isTest){
	baseUrl = 'https://tst.shengejiaju.cn'
}else{
	baseUrl = 'https://master.shengejiaju.cn'
}
Vue.prototype.$domain = baseUrl

App.mpType = 'app'

// #ifdef H5
import wx from '@/static/utils/wx-jssdk.js'
Vue.prototype.$wx = wx
// #endif

const errorHandler = (err, vm, info) => {
	console.log(err.message)
	console.log(err.stack)
	console.log(getCurrentPages()[getCurrentPages().length - 1].options)
	console.log(getCurrentPages()[getCurrentPages().length - 1].route)
	let path = getCurrentPages()[getCurrentPages().length - 1].route.split('/')
	uni.request({
		url: `${baseUrl}/api/User/SetError`,
		method: 'POST',
		header: {
			'content-type':'application/x-www-form-urlencoded'
		},
		data: {
			key: sign().key,
			port: sign().port,
			sign: sign().sign,
			timestamp: sign().timestamp,
			message: '前端JS报错',
			path: path[path.length-1],
			info: err.stack
		},
		success: () => {
			
		},
		fail: () => {
			
		}
	})
}

Vue.config.errorHandler = errorHandler;  
Vue.prototype.$throw = (error) => errorHandler(error, this);

// 引入全局uView
import uView from 'uview-ui'
Vue.use(uView);

// 兼容某些API在H5上也能使用
import '@/static/utils/ican-H5Api'

const app = new Vue({
	store,
  ...App
})

import httpInterceptor from '@/api/http.interceptor.js'
Vue.use(httpInterceptor, app)

import httpApi from '@/api/http.api.js'
Vue.use(httpApi, app)

import httpApi1 from '@/api/http.api-1.js'
Vue.use(httpApi1, app)

let mpShare = require('uview-ui/libs/mixin/mpShare.js');
Vue.mixin(mpShare)

app.$mount()